import { changeNumMoneyToChinese, getColorTextLoadMap, getDeptPosTreeIcon, getKeyRouter, getUserShortCode } from '@/utils';
import moment from 'moment';
import { useEffect } from 'react';
import { INeedActionEnum } from '../common';

/**
 * 处理JSON中日期格式
 * @param timeList
 * @param timeName
 * @returns
 */
const handListMoment = (timeList: Record<string, any>[], timeName: string[], momentType: boolean) => {
  let momentList: Record<string, any>[] = [];
  timeList?.forEach((itemData: any) => {
    let itemObj: Record<string, any> = { ...itemData };
    timeName?.forEach((itemCode: any) => {
      if (itemCode && itemObj?.[itemCode]) {
        if (momentType) {
          itemObj[itemCode] = moment(itemObj?.[itemCode])?.format('YYYY-MM-DD HH:mm:ss');
        } else {
          itemObj[itemCode] = moment(itemObj?.[itemCode]);
        }
      }
    });
    momentList?.push(itemObj);
  });

  return momentList;
};

/** OA详情监听渲染 */
const needInfoEffect = (detailInfo: Record<string, any>, needForm: any) => {
  /** 初始化数据 */
  useEffect(() => {
    if (detailInfo && detailInfo?.id) {
      const newDetailInfo: Record<string, any> = {
        ...detailInfo,
        needName: detailInfo?.needName,
        fullName: detailInfo?.fullName,
        // preBeginTime: detailInfo?.preBeginTime ? moment(detailInfo?.preBeginTime) : null,
        // preFinishTime: detailInfo?.preFinishTime ? moment(detailInfo?.preFinishTime) : null,
        // realBeginTime: detailInfo?.realBeginTime ? moment(detailInfo?.realBeginTime) : null,
        // realFinishTime: detailInfo?.realFinishTime ? moment(detailInfo?.realFinishTime) : null,
        // detailList:
        //   detailInfo?.detailList?.length > 0
        //     ? handListMoment(detailInfo?.detailList, ['deliveryTime'], false)
        //     : [],
        // payList: detailInfo?.payList?.length > 0 ? handListMoment(detailInfo?.payList, ['payTime'], false) : [],
        // tempContractURL: detailInfo?.tempContractURL,
        // contractURL: detailInfo?.contractURL,
        // sumInTaxCN: detailInfo?.sumInTax ? changeNumMoneyToChinese(detailInfo?.sumInTax) : '零',
      };
      needForm?.setFieldsValue({
        ...newDetailInfo,
      });
    }
  }, [detailInfo?.id]);
};

export { handListMoment, needInfoEffect };
